package com.zrm.zfcrpa.configuration.security.mobile;

import com.zrm.zfcrpa.configuration.security.MyUserDetals;
import com.zrm.zfcrpa.dao.mapper.loginMapper.LoginDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

@Component
public class SmsCodeAuthConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Autowired
    private MyUserDetals myUserDetals;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginDao loginDao;

    @Override
    public void configure(HttpSecurity builder) throws Exception {
        super.configure(builder);

        SmsCodeAuthenticationFilter smsCodeAuthenticationFilter = new SmsCodeAuthenticationFilter(redisTemplate,loginDao,myUserDetals);
        smsCodeAuthenticationFilter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));

        SmsCodeAuthProvider smsCodeAuthProvider = new SmsCodeAuthProvider();
        smsCodeAuthProvider.setMyUserDetals(myUserDetals);

        builder.authenticationProvider(smsCodeAuthProvider)
                .addFilterAfter(smsCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

    }
}
